//
//  ZYXStudyDetailVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/10.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXStudyDetailVC.h"

#import "ZYXStudyDetHeaderView.h"
#import "ZYXStudyDetCell.h"
#import "ZYXMsgDetHeaderView.h"
#import "SeachField.h"
#import "KTLOVEComment.h"
#import "ZYXMeaasageModel.h"
#import "ZYXHuifuVC.h"
#import "ZYXStudyModel.h"
#import "ZYXStudyHeaderView.h"
@interface ZYXStudyDetailVC ()<ZYXStudyDetCellDelegate>
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet SeachField *textFeild;
@property (strong,nonatomic) KTLOVEComment *commentView;
@property (strong,nonatomic) ZYXStudyHeaderView *headerView;
@property (strong,nonatomic) NSMutableArray *dataArr;
@property (strong,nonatomic) ZYXStudyModel *model;
///页数
@property (strong,nonatomic) NSString *page;
@end

@implementation ZYXStudyDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学习头条";
    self.tableView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-HEIGHT(45));
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXStudyDetCell" bundle:nil] forCellReuseIdentifier:@"ZYXStudyDetCell"];
//    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXMsgDetHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"ZYXMsgDetHeaderView"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXStudyHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"ZYXStudyHeaderView"];
    self.tableView.estimatedRowHeight = HEIGHT(51);

    self.headerView = [[NSBundle mainBundle] loadNibNamed:@"ZYXStudyDetHeaderView" owner:nil options:nil].lastObject;
    self.headerView.height = 100;
    self.tableView.tableHeaderView = self.headerView;
    [self.view addSubview:self.tableView];
    self.textFeild.text = @"写评论";
    UIImageView *searchImgView = [[UIImageView alloc] initWithImage:IMG(@"xiepinglun")];
    self.textFeild.leftView = searchImgView;
    self.textFeild.leftViewMode = UITextFieldViewModeAlways;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeHeight:) name:@"ceshigaodu" object: nil];
    
    [self setupRefresh];
    self.page = @"1";
    [self loadRequestPage:@"1"];
}
-(void)changeHeight:(NSNotification *)not{
      NSString *str = (NSString *)not.object;
    
    self.headerView.height = str.floatValue;
    [self.tableView reloadData];
}
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return NO;
}
// 显示空白页 可以滑动
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}
#pragma mark 加载数据
- (void)setupRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        [self.dataArr removeAllObjects];
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}
- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
- (void)loadRequestPage:(NSString *)page {
    if ([UID integerValue] ==0) {
        [ZYXLoginViewController setupLogin];
        return;
    }
   
    NSLog(@"%@",@{@"id":self.uid,@"user_id":UID,@"page":self.page});
    [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_IndexNewLst) parmas:@{@"id":self.uid,@"user_id":UID,@"page":self.page} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"responseObject=%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            self.model = [[ZYXStudyModel alloc]init];
            self.model.uid = self.uid;
            self.model.count = [NSString stringWithFormat:@"%@",responseObject[@"count"]];
            self.model.content_url = responseObject[@"data"][@"content_url"];
            self.countLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"count"]];
            self.headerView.model = self.model;
            if ([self.page intValue]<= [responseObject[@"info"][@"last_page"] intValue]) {
                NSArray *modelArr = [ZYXStudyInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"info"][@"data"]];
                [self.dataArr addObjectsFromArray:modelArr];
            }
              [self.tableView reloadData];
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
            [self endRefresh];
    } failureBlock:^(NSError *error) {
            [self endRefresh];
    }];

}
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ZYXStudyDetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXStudyDetCell"];
    cell.model = self.dataArr[indexPath.row];
    cell.indexPath = indexPath;
    cell.delegate =self;
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  UITableViewAutomaticDimension;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYXStudyInfoModel *model = self.dataArr[indexPath.row];
    if ([model.res isEqualToString:@"1"]) {
        ZYXHuiFuVC *vc = [[ZYXHuiFuVC alloc]init];
        vc.list_id = model.uid;         [self.navigationController pushViewController:vc animated:YES];
    }else{
        [JKToast showWithText:@"暂时没有管理员回复"];
    }
}
#pragma mark 点击了评论
- (IBAction)clickCommentBtn:(UIButton *)sender {
    WEAK(weakSelf);
    [self.commentView showView];
    self.commentView.determineClickBlock = ^(NSString *str) {
        NSString *fullStr = [Functions trimAndLowercase:str];
        if (fullStr.length) {
            [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_IndexNewsComment) parmas:@{@"news_id":weakSelf.uid,@"user_id":UID,@"content":str} successBloock:^(NSDictionary *responseObject) {
                NSLog(@"responseObject=%@",responseObject);
                if ([responseObject[@"code"] isEqualToNumber:@200]) {
                    [weakSelf.dataArr removeAllObjects];
                    weakSelf.page = @"1";
                    [weakSelf loadRequestPage:@"1"];
                    
                }else{
                    [JKToast showWithText:responseObject[@"message"]];
                }
                
                
            } failureBlock:^(NSError *error) {
                
            }];
        }else{
              [JKToast showWithText:@"您评论的内容为空"];
        }
       

        
        
        
        
    };
}
#pragma mark ZYXStudyDetCellDelegate
-(void)clickHuiFu:(NSIndexPath *)indexPath{
    ZYXStudyInfoModel *model = self.dataArr[indexPath.row];
    if ([model.res isEqualToString:@"1"]) {
        ZYXHuiFuVC *vc = [[ZYXHuiFuVC alloc]init];
        vc.list_id = model.uid;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [JKToast showWithText:@"暂时没有管理员回复"];
    }
   
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
//        ZYXMeaasageModel *model0 = [[ZYXMeaasageModel alloc]initWitNameStr:@"管理员" content:@"哈哈哈哈哈"];
//        ZYXMeaasageModel *model1 = [[ZYXMeaasageModel alloc]initWitNameStr:@"管理员" content:@"全世界华人团结起来，早日实现中华民族的伟大复兴，我们全世界华人在世人面前才有话语权,在世界的任何地方才不被欺负！"];
//        ZYXMeaasageModel *model2 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我员" content:@"有道理"];
//        ZYXMeaasageModel *model3 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我员" content:@"你真美"];
//        ZYXMeaasageModel *model4 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我" content:@"哈哈哈哈哈哈哈哈哈哈哈哈哈哈"];
//        ZYXMeaasageModel *model5 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我" content:@"哈哈哈哈哈好好好好哈哈哦啊好好哦好好哦啊爱护哦啊好好好哦嗷嗷好哦好"];
//        ZYXMeaasageModel *model6 = [[ZYXMeaasageModel alloc]initWitNameStr:@"管理我就是我" content:@"哈哈哈哈哈"];
//        _dataArr = [NSMutableArray arrayWithObjects:model0,model1,model2,model3,model4,model5,model6, nil];
        
    }
    return _dataArr;
}

#pragma mark 懒加载
- (KTLOVEComment *)commentView{
    if (!_commentView) {
        _commentView = [[KTLOVEComment alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    return _commentView;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([UID integerValue] >0) {
   
    }else{
        [ZYXLoginViewController setupLogin];
    }
    
}


@end
